using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Collections;
using System.Data.Odbc;
namespace CocaCola_Inventory_System.Forms
{
    public partial class Replicate : Form
    {
        public Replicate()
        {
            InitializeComponent();
        }

        private void Replicate_Load(object sender, EventArgs e)
        {
            if (((CocaCola_Inventory_System.Forms.Container)this.MdiParent).security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            if (!backgroundWorker1.IsBusy)
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           /* DirectoryEntry ode1 = new DirectoryEntry(@"dc=AFR,dc=ko,dc=com");
            DirectorySearcher searcher1 = new DirectorySearcher(ode1);
            searcher.Filter = "(&(objectClass=user))";
            searcher1.SearchScope = SearchScope.Subtree;
            SearchResultCollection res1 = searcher1.FindAll();
            MessageBox.Show(res1.Count.ToString());
            return;

            DirectoryEntry ode = new DirectoryEntry(@"LDAP://OU=Users,OU=EG-CAI,OU=AFR,dc=AFR,dc=ko,dc=com");

            DirectorySearcher searcher = new DirectorySearcher(ode);
            searcher.Filter = "(&(objectClass=user))";
            searcher.SearchScope = SearchScope.Subtree;
            SearchResultCollection res = searcher.FindAll();

            OdbcConnection con = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=10.87.212.173;Port=3306;Database=inventorydb;User=root; Password=kym07;Option=3;");

            OdbcCommand com = new OdbcCommand();
            com.Connection = con;
            con.Open();
            // cn,PostalAddress,postalCode,department,departmentNumber,telephoneNumber,mobile,title,
            //extensionAttribute2,mail,
            string cn = "";
            string PostalAddress = "";
            string postalCode = "";
            string department = "";
            string departmentNumber = "";
            string telephoneNumber = "";
            string mobile = "";
            string extensionAttribute2 = "";
            string title = "";
            string mail = "";
            int sum = res.Count;
            int done = 0;
            foreach (SearchResult r in res)
            {
                /*string cn = "";
                string PostalAddress = "";
                string postalCode = "";
                string department = "";
                string departmentNumber = "";
                string telephoneNumber = "";
                string mobile = "";
                string extensionAttribute2 = "";
                string title = "";
                string mail = "";
                DirectoryEntry dir = new DirectoryEntry(r.Path);

                com.CommandText = "select count(*) from users where loginid like  '" + dir.Properties["samaccountname"].Value + "'";
                int count = Convert.ToInt32(com.ExecuteScalar());
                if (count > 0)
                {
                    com.CommandText = "update users set ";

                    com.CommandText += "name = " + dir.Properties["cn"].Value;
                    com.CommandText += ",title = " + dir.Properties["title"].Value;
                    com.CommandText += ",department = " + dir.Properties["department"].Value;
                    // com.CommandText += "(name";
                    if (dir.Properties["mobile"].Value != null)
                        com.CommandText += ",mobile =" + dir.Properties["mobile"].Value;

                    if (dir.Properties["PostalCode"].Value != null)
                        com.CommandText += ",Postal_Code =" + dir.Properties["postalcode"].Value;

                    if (dir.Properties["postaladdress"].Value != null)
                        com.CommandText += ",postal_address =" + dir.Properties["postaladdress"].Value;

                    if (dir.Properties["telephoneNumber"].Value != null)
                        com.CommandText += ",telephone =" + dir.Properties["telephonenumber"].Value;

                    if (dir.Properties["extensionAttribute2"].Value != null)
                        com.CommandText += ",extension_Attribute2 =" + dir.Properties["extensionAttribute2"].Value;

                    if (dir.Properties["mail"].Value != null)
                        com.CommandText += ",Email =" + dir.Properties["mail"].Value;

                    if (dir.Properties["departmentNumber"].Value != null)
                        com.CommandText += ",department_no =" + dir.Properties["departmentNumber"].Value;

                    if (dir.Properties["l"].Value != null)
                        com.CommandText += ",city =" + dir.Properties["l"].Value;

                    if (dir.Properties["c"].Value != null)
                        com.CommandText += ",country =" + dir.Properties["c"].Value; ;

                    com.CommandText += "  where Loginid like '" + dir.Properties["sAMAccountName"].Value + "'";
                }
                else
                {
                    com.CommandText = " insert into users ";

                    //list[i] = ( new pair(dir.Properties["cn"].Value.ToString(), dir.Properties["sAmAccountType"].Value.ToString()));
                    //listBox1.Items.Add(dir.Properties["cn"].Value.ToString());
                    com.CommandText += "(name,title,department";
                    // com.CommandText += "(name";
                    if (dir.Properties["mobile"].Value != null)
                        com.CommandText += ",mobile";

                    if (dir.Properties["PostalCode"].Value != null)
                        com.CommandText += ",Postal_Code";

                    if (dir.Properties["postaladdress"].Value != null)
                        com.CommandText += ",postal_address";

                    if (dir.Properties["telephoneNumber"].Value != null)
                        com.CommandText += ",telephone";

                    if (dir.Properties["extensionAttribute2"].Value != null)
                        com.CommandText += ",extension_Attribute2";

                    if (dir.Properties["mail"].Value != null)
                        com.CommandText += ",Email";

                    if (dir.Properties["departmentNumber"].Value != null)
                        com.CommandText += ",department_no";

                    if (dir.Properties["l"].Value != null)
                        com.CommandText += ",city";

                    if (dir.Properties["c"].Value != null)
                        com.CommandText += ",country";

                    if (dir.Properties["sAMAccountName"].Value != null)
                        com.CommandText += ",loginid";


                    //if(dir.Properties["mobile"].Value!="")
                    //com.CommandText+=",mobile";


                    //,Postal_code,postal_address";
                    com.CommandText += " )Values (";
                    com.CommandText += "'" + dir.Properties["cn"].Value + "','" + dir.Properties["title"].Value + "','" + dir.Properties["department"].Value + "'";
                    //com.CommandText += "'" + dir.Properties["cn"].Value + "','";
                    if (dir.Properties["mobile"].Value != null)
                        com.CommandText += ",'" + dir.Properties["mobile"].Value + "'";

                    if (dir.Properties["PostalCode"].Value != null)
                        com.CommandText += ",'" + dir.Properties["PostalCode"].Value + "'";

                    if (dir.Properties["postaladdress"].Value != null)
                        com.CommandText += ",'" + dir.Properties["postaladdress"].Value + "'";

                    if (dir.Properties["telephoneNumber"].Value != null)
                        com.CommandText += ",'" + dir.Properties["telephoneNumber"].Value + "'";

                    if (dir.Properties["extensionAttribute2"].Value != null)
                        com.CommandText += ",'" + dir.Properties["extensionAttribute2"].Value + "'";

                    if (dir.Properties["mail"].Value != null)
                        com.CommandText += ",'" + dir.Properties["mail"].Value + "'";

                    if (dir.Properties["departmentNumber"].Value != null)
                        com.CommandText += ",'" + dir.Properties["departmentNumber"].Value + "'";
                    /*if (dir.Properties["postalCode"].Value != null)
                        com.CommandText += ",'" + dir.Properties["postal_code"].Value + "'";
                    if (dir.Properties["postaladdress"].Value != null)
                        com.CommandText += ",'" + dir.Properties["postal_address"].Value + "'";
                    
                    if (dir.Properties["l"].Value != null)
                        com.CommandText += ",'" + dir.Properties["l"].Value + "'";

                    if (dir.Properties["c"].Value != null)
                        com.CommandText += ",'" + dir.Properties["c"].Value + "'";
                    if (dir.Properties["sAMAccountName"].Value != null)
                        com.CommandText += ",'" + dir.Properties["sAMAccountName"].Value + "'";
                    com.CommandText += ")";
                    com.ExecuteNonQuery();

                }
                done++;

                backgroundWorker1.ReportProgress((done / sum)*100);




            }
        
           // con.Close();

            
            ArrayList alDomains = new ArrayList();
            Domain my = Domain.GetComputerDomain();
            foreach(Domain d in my.Children)
            {
                listBox1.Items.Add(d);
            }

            */
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Replication Completed SuccessFully");
            this.Dispose();

        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            progressBar1.Update();
        }

        private void Replicate_FormClosing(object sender, FormClosingEventArgs e)
        {
            backgroundWorker1.CancelAsync();
        }
        }
    }
